package com.googlecode.jnarwhal.world;

import java.util.ArrayList;

/**
 * 
 * @author Pivo
 * 
 */

public class World {
	
	private static final int CHUNKSIZEX = 64;
	private static final int CHUNKSIZEY = 128;
	private static final int CHUNKSIZEZ = 64;
	private static final int WORLDSIZEX = 100;
	private static final int WORLDSIZEZ=100;
	private static final int STARTCHUNKSX = 1;
	private static final int STARTCHUNKSZ = 1;
	public ArrayList<Chunk> chunks;
	public Chunk world[] ;
	
	public World() {
		world= new Chunk[WORLDSIZEX*WORLDSIZEZ];
		chunks = new ArrayList<Chunk>();
	

	}

	/**
	 * Called to generate a the World presets are 3x3 Chunks
	 * 
	 * @param seed
	 */
	public void generateWorld(int seed) {

		for (int i = -STARTCHUNKSX; i <=STARTCHUNKSX ; i++) {
			for (int j = -STARTCHUNKSZ; j <= STARTCHUNKSZ; j++) {
				
				System.out.println(i +" "+j);
				
				Chunk newChunk = new Chunk(i, j);
				newChunk.generateChunk(seed);
				chunks.add(newChunk);
				
				
			}

		}

	}
	
	
	public void destroyBlockAt(int x , int y, int z){
		Block block;
		int chunkX, chunkZ;
		
		
		
		for(int i = 0; i < chunks.size(); i++){
			
			Chunk chunk = chunks.get(i);
			chunkX = chunk.getX() ;
			chunkZ = chunk.getZ();
			
			if( chunkX == (int)(x/CHUNKSIZEX) &&   chunkZ == (int)(z/CHUNKSIZEZ)){
				
				x -= (int)(x/CHUNKSIZEX) *CHUNKSIZEX ;
				z -= (int)(z/CHUNKSIZEZ)*CHUNKSIZEZ ;
				
				 chunk.destroyBlockAt(x, y, z);
			}
		}
		
	}
	
	
	/**
	 * called to get a Block at a specific coordinate
	 * @param x
	 * @param y
	 * @param z
	 * @return the Block
	 */
	
	public Block getBlockAt(int x, int y, int z){
		
		Block block;
		int chunkX, chunkZ;
		
		
		
		for(int i = 0; i < chunks.size(); i++){
			
			Chunk chunk = chunks.get(i);
			chunkX = chunk.getX() ;
			chunkZ = chunk.getZ();
			
			if( chunkX == (int)(x/CHUNKSIZEX) &&   chunkZ == (int)(z/CHUNKSIZEZ)){
				
				x -= (int)(x/CHUNKSIZEX) *CHUNKSIZEX ;
				z -= (int)(z/CHUNKSIZEZ)*CHUNKSIZEZ ;
				
				return chunk.getBlockAt(x, y, z);
			}
		}
		
	
		
		
		return null;
	}

}
